package com.example.leo.mycharmingstudent.a0831;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Leo on 2020/8/31.
 */

public class MenuFragment extends Fragment {

    private List<Food> mFoodList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.menu_fragment, null, false);
        /*
        Fragment获取参数的步骤
        1.getArguments获取bundle对象
        2.bundle对象调用getString等方法
         */
        Bundle arguments = getArguments();
        String title = null;
        if (arguments != null) {
            title = arguments.getString("title");
//arguments.getInt()此处和
//MainPageActivity文件中的bundle.putInt();相对应
        }
        TextView tvTitle = inflate.findViewById(R.id.tv_title_menu_fragment);
        ListView lvFood = inflate.findViewById(R.id.lv_menu_fragment);

        //初始化食物数据
        for (int i = 0; i < 10; i++) {
            Food cocacola = new Food("cocacola", "42元");
            mFoodList.add(cocacola);
        }

        tvTitle.setText(TextUtils.isEmpty(title) ?
                "默认的MenuFragment标题" : title);
        MyAdapter myAdapter = new MyAdapter();
        lvFood.setAdapter(myAdapter);
        //注意，此处一定不要使用默认的代码，return 用自己创建的View
        //如果使用默认的代码，界面空白
//        return super.onCreateView(inflater, container, savedInstanceState);
        return inflate;
    }

    public static class Food {
        private String foodName;
        private String foodPrice;

        public Food(String foodName, String foodPrice) {
            this.foodName = foodName;
            this.foodPrice = foodPrice;
        }

        public String getFoodName() {
            return foodName;
        }

        public void setFoodName(String foodName) {
            this.foodName = foodName;
        }

        public String getFoodPrice() {
            return foodPrice;
        }

        public void setFoodPrice(String foodPrice) {
            this.foodPrice = foodPrice;
        }
    }

    class MyAdapter extends BaseAdapter {

        /**
         * 多少个条目
         */
        @Override
        public int getCount() {
            return mFoodList.size();
        }

        /**
         * 获取position位置的数据源，
         * 也就是该位置的数据对象
         */
        @Override
        public Object getItem(int position) {
            return mFoodList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        /**
         * getView方法内部实现流程（基础操作）
         * 第一步，判断convertView是否为null，
         * 如果为null，就使用LayoutInflater创建一个View
         * 第二步，findViews---->findViewById查找所有控件
         * 第三步，给控件设置值
         *
         * 注意：return返回的不是null，是自己使用LayoutInflater创建的View
         */
        @Override
        public View getView(
                int position,
                View convertView,
                ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater
                        .from(getContext())
                        .inflate(R.layout.item_menu_menu_fragment, null, false);
            }
            TextView tvFoodName = convertView.findViewById(R.id.tv_food_name_item);
            TextView tvFoodPrice = convertView.findViewById(R.id.tv_food_price_item);

            Food food = (Food) getItem(position);
            tvFoodName.setText(food.getFoodName());
            tvFoodPrice.setText(String.format("价格是：%s", food.getFoodPrice()));
            return convertView;
        }
    }
}
